package com.medical.doctor.service.impl;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.medical.common.security.utils.SecurityUtils;
import com.medical.doctor.domain.*;
import com.medical.doctor.dto.DrugAndPrescriptionDTO;
import com.medical.doctor.vo.ReceiveVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.medical.doctor.mapper.ReceivePatientsMapper;
import com.medical.doctor.service.IReceivePatientsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 接诊Service业务层处理
 *
 * @author medical
 * @date 2024-07-21
 */
@Service
public class ReceivePatientsServiceImpl implements IReceivePatientsService
{
    @Autowired
    private ReceivePatientsMapper receivePatientsMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询接诊
     *
     * @param id 接诊主键
     * @return 接诊
     */
    @Override
    public ReceivePatients selectReceivePatientsById(Long id)
    {
        return receivePatientsMapper.selectReceivePatientsById(id);
    }

    /**
     * 查询接诊列表
     *
     * @param receivePatients 接诊
     * @return 接诊
     */
    @Override
    public List<ReceiveVo> selectReceivePatientsList(ReceivePatients receivePatients)
    {
        Long userId = SecurityUtils.getLoginUser().getUserid();
        receivePatients.setUserId(userId);
        return receivePatientsMapper.selectReceivePatientsList(receivePatients);
    }

    /**
     * 新增接诊
     *
     * @param receivePatients 接诊
     * @return 结果
     */
    @Override
    public int insertReceivePatients(ReceivePatients receivePatients)
    {
        return receivePatientsMapper.insertReceivePatients(receivePatients);
    }

    /**
     * 修改接诊
     *
     * @param receivePatients 接诊
     * @return 结果
     */
    @Override
    public int updateReceivePatients(ReceivePatients receivePatients)
    {
        return receivePatientsMapper.updateReceivePatients(receivePatients);
    }

    /**
     * 批量删除接诊
     *
     * @param ids 需要删除的接诊主键
     * @return 结果
     */
    @Override
    public int deleteReceivePatientsByIds(Long[] ids)
    {
        return receivePatientsMapper.deleteReceivePatientsByIds(ids);
    }

    /**
     * 删除接诊信息
     *
     * @param id 接诊主键
     * @return 结果
     */
    @Override
    public int deleteReceivePatientsById(Long id)
    {
        return receivePatientsMapper.deleteReceivePatientsById(id);
    }

    @Override
    public int updateDoctorStatus(Integer status) {
        Long userId = SecurityUtils.getUserId();
        return receivePatientsMapper.updateDoctorStatus(userId,status);
    }

    @Override
    public List<Drug> selectDrugList(String drugName) {
        return receivePatientsMapper.selectDrugList(drugName);
    }

    @Override
    public Long selectPatientsById(Long id) {
        Long l = receivePatientsMapper.selectMedicalMedicById(id);
        return l;
    }

    @Override
    @Transactional
    public Long perscriptionAdd(PreAndDiagnostic preAndDiagnostic) {
        List<DrugAndPrescriptionDTO> drugs = preAndDiagnostic.getDrugs();
        Date date = new Date();
        long prescriptionId = date.getTime();
        //定义总金额
        BigDecimal totalSum=BigDecimal.ZERO;
        totalSum = getBigDecimal(drugs, prescriptionId, totalSum);
        //根据接诊台id查询信息
        Long id = preAndDiagnostic.getId();
        ReceivePatients patients=receivePatientsMapper.findReceivePatientsById(id);
        //获取医生信息
        MedicalMedic medicalMedic=receivePatientsMapper.findHospitalIdAndDepartmentId(patients.getDoctorId());
        //添加诊断记录表
        DiagnosticRecord diagnosticRecord = new DiagnosticRecord();
        diagnosticRecord=extracted(preAndDiagnostic, patients, prescriptionId, medicalMedic, totalSum);

//        rabbitTemplate.convertAndSend("send_prescriptionInfo",diagnosticRecord.getDiagnoseId(),message -> {
//            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
//            return message;
//        });
        return diagnosticRecord.getDiagnoseId();
    }


    private DiagnosticRecord extracted(PreAndDiagnostic preAndDiagnostic, ReceivePatients patients, long prescriptionId, MedicalMedic medicalMedic, BigDecimal totalSum) {
        //完成诊断记录表的赋值生成
        DiagnosticRecord diagnosticRecord = new DiagnosticRecord();
        diagnosticRecord.setDoctorId(patients.getDoctorId());
        diagnosticRecord.setPatientId(patients.getPatientId());
        diagnosticRecord.setDiagnoseSis(preAndDiagnostic.getPrescription());
        diagnosticRecord.setPrescriptionId(prescriptionId);
        diagnosticRecord.setHospitalId(medicalMedic.getHospitalId());
        diagnosticRecord.setDepartmentId(medicalMedic.getDepartmentIds());
        //计算诊断金额
        diagnosticRecord.setDiagnosePrice(totalSum);
        receivePatientsMapper.diagnosticRecordAdd(diagnosticRecord);
        return diagnosticRecord;
    }

    private BigDecimal getBigDecimal(List<DrugAndPrescriptionDTO> drugs, long prescriptionId, BigDecimal totalSum) {
        //完成处方表的生成
        for (DrugAndPrescriptionDTO drug : drugs) {
            drug.setPrescriptionId(prescriptionId);
            receivePatientsMapper.perscriptionAdd(drug);

            DrugAndPrescriptionDTO drug1=receivePatientsMapper.findDrugByDrugId(drug.getDrugId());
            int quantity=drug.getDrugsNumber();
            BigDecimal price=drug1.getDrugPrice();
            //计算当前药品的总价
            BigDecimal totalPriceForDrug=price.multiply(BigDecimal.valueOf(quantity));
            //累加到总和中
            totalSum = totalSum.add(totalPriceForDrug);
        }
        return totalSum;
    }
}
